/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sjtu.servlet;

import com.sjtu.bean.PurchaseRequestBean;
import com.sjtu.bean.UserBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author p w
 */
public class PurchaserequetListServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doWork(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doWork(request, response);
    }

    public void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String obj = request.getParameter("obj");
        JSONObject jsonObj = null;
        try {
            jsonObj = new JSONObject(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PrintWriter writer = response.getWriter();
        try {
            int reqType = jsonObj.getInt("reqType");
            ArrayList<PurchaseRequestBean> requestList = PurchaseRequestBean.getPurchaseRequest(reqType);
            JSONObject jsonRes = new JSONObject();
            ArrayList<Integer> requsetIdList = new ArrayList<Integer>();
            ArrayList<String> bookNameList = new ArrayList<String>();
            ArrayList<String> commentsList = new ArrayList<String>();
            ArrayList<Integer> scoreList = new ArrayList<Integer>();
            ArrayList<String> isbnList = new ArrayList<String>();
            ArrayList<String> authorList = new ArrayList<String>();
            ArrayList<String> publisherList = new ArrayList<String>();
            ArrayList<Date> createDateList = new ArrayList<Date>();
            ArrayList<Integer> userIdList = new ArrayList<Integer>();
            for(int i=0;i<requestList.size();i++){
                PurchaseRequestBean req = requestList.get(i);
                requsetIdList.add(req.getId());
                bookNameList.add(req.getBookName());
                commentsList.add(req.getComments());
                scoreList.add(req.getScore());
                isbnList.add(req.getIsbn());
                authorList.add(req.getAuthor());
                publisherList.add(req.getPublisher());
                createDateList.add(req.getCreateDate());
                userIdList.add(req.getUserId());
            }
            jsonRes.put("bookNameList", bookNameList);
            jsonRes.put("commentsList", commentsList);
            jsonRes.put("scoreList", scoreList);
            jsonRes.put("isbnList", isbnList);
            jsonRes.put("authorList", authorList);
            jsonRes.put("publisherList", publisherList);
            jsonRes.put("createDateList", createDateList);
            jsonRes.put("requsetIdList", requsetIdList);
            ArrayList<String> userNameList = new ArrayList<String>();
            for (int i = 0; i < userIdList.size(); i++) {
                System.out.println("Here"+userIdList.get(i));
                System.out.println(UserBean.getUser(userIdList.get(i)).getName());
                userNameList.add(UserBean.getUser(userIdList.get(i)).getName());
            }
            System.out.println("userNameList" + userNameList);
            jsonRes.put("userNameList", userNameList);
            System.out.println(jsonRes);
            writer.write(jsonRes.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        writer.flush();
        writer.close();
    }
}
